-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Collection<T>: Validate parameters in the public methods #23166
Conversation
I was looking over the new Range methods on `Collection<T>` and it occurred to me that for all existing methods, parameter validation is done in the *public* methods before calling the *protected virtual* methods. This changes the new Range methods to do the same.
Sounds like a good idea for a couple of reasons:
|
I agree, this will solve some problems we are already having in the CoreFX project. |
|
||
if (collection == null) | ||
{ | ||
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no list
argument to this method, but the exception is going to be new ArgumentNullException("list")
. The ExceptionArgument.list
should be changed to ExceptionArgument.collection
.
|
||
if ((uint)index > (uint)items.Count) | ||
{ | ||
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This exception message is "Index must be within the bounds of the List.". The code consuming this isn't using "List", though.
|
||
if (index > items.Count - count) | ||
{ | ||
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exception message here is "Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection"... but the arguments to this method are "index" and "count", not "offset" and "length"... are we ok with that mismatch?
|
||
if (index > items.Count - count) | ||
{ | ||
ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidOffLen); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as earlier.
|
||
if (collection == null) | ||
{ | ||
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.list); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment as earlier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The actual changes here LGTM. But some of the previous code that's being moved is suspect, in particular with regards to the contents of the exceptions being thrown. Let's merge this, but then we should follow up to get those exceptions fixed as is appropriate.
@ahoefling, are you planning to follow-up with a PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @justinvp
@justinvp I have some time today and will work on a PR for this based on the feedback above |
…clr#23166) I was looking over the new Range methods on `Collection<T>` and it occurred to me that for all existing methods, parameter validation is done in the *public* methods before calling the *protected virtual* methods. This changes the new Range methods to do the same. Commit migrated from dotnet/coreclr@5231179
I was looking over the new Range methods on
Collection<T>
(added in #23018) and it occurred to me that for all existing methods, parameter validation is done in the public methods before calling the protected virtual methods. This changes the new Range methods to do the same.cc: @ahoefling, @ahsonkhan, @safern